﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace WebStudio.Framework.Helper
{
    /// <summary>
    /// Json序列化工具类
    /// </summary>
    public class JsonHelper
    {
        static JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

        #region 序列化对象函数
        /// <summary>
        /// 序列化对象函数
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(object obj)
        {
            return jsonSerializer.Serialize(obj);
        }
        #endregion

        #region 反序列化对象函数
        /// <summary>
        /// 反序列化对象函数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string jsonString)
        {
            try
            {
                return jsonSerializer.Deserialize<T>(jsonString);
            }
            catch (Exception)
            {
            }
            return default(T);
        }
        #endregion
         
    }
} 
 